<?php

namespace common\models\db;

use Yii;
use common\components\wtools\tools\ArrayHelper;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%base_areas}}".
 *
 * @author
 */
class BaseAreas extends \common\models\db\tables\BaseAreas
{
    const SCENARIO_TEST = 'test';


    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors = ArrayHelper::merge($behaviors, [
            'timestamp' => [
                'class' => TimestampBehavior::class,
                'createdAtAttribute' => false,
                'updatedAtAttribute' => false,
            ],
            'blameable' => [
                'class' => BlameableBehavior::class,
                'createdByAttribute' => false,
                'updatedByAttribute' => false,
            ],
        ]);
        return $behaviors;
    }

    public function attributeLabels()
    {
        $attributeLabels = parent::attributeLabels();
        $attributeLabels = ArrayHelper::merge($attributeLabels, []);
        return $attributeLabels;
    }

    public function rules()
    {
        $rules = parent::rules();
        /*foreach ($rules as $k => $v) {
            if ($v[1] == 'required'){
                $rules[$k][0] = array_diff($rules[$k][0], ['created_at', 'updated_at', 'created_by', 'updated_by']);
            }
        }*/
        $rules = ArrayHelper::merge($rules, [
//            [[], 'required', 'on' => self::SCENARIO_TEST],
        ]);
        return $rules;
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios = ArrayHelper::merge($scenarios, [
            self::SCENARIO_TEST => [],
        ]);
        return $scenarios;
    }

    const LEVEL_0 = 0;
    const LEVEL_1 = 1;
    const LEVEL_2 = 2;
    const LEVEL_3 = 3;
    const LEVEL_4 = 4;
    const LEVEL_5 = 5;
    const ID_CHINA = 100000000000;

    public static function getLevelDatas()
    {
        return [
            self::LEVEL_0 => ["中国(china)"],
            self::LEVEL_1 => ["省份(province)或直辖市"],
            self::LEVEL_2 => ["城市(city)"],
            self::LEVEL_3 => ["县或地区(county)"],
            self::LEVEL_4 => ["乡镇(town)"],
            self::LEVEL_5 => ["村庄(village)"],
        ];
    }

    /**
     * @return array
     */
    public static function getGangAoTaiProIds()
    {
        return [
            710000000000, // 台湾
            810000000000, // 香港特别行政区
            820000000000, // 澳门特别行政区
        ];
    }

    /**
     * @return false|mixed|BaseAreas[]
     */
    public static function getAllWithCache()
    {
        return Yii::$app->cache->getOrSet("db-BaseAreas-getAllWithCache", function () {
            return static::find()->all();
        }, null);
    }

    /**
     * @param $id
     * @return false|BaseAreas|mixed
     */
    public static function getByIdWithCache($id)
    {
        return Yii::$app->cache->getOrSet("db-BaseAreas-getByIdWithCache/{$id}", function () use ($id) {
            return static::findOne($id);
        }, null);
    }

    public function deleteCaches()
    {
        Yii::$app->cache->delete("db-BaseAreas-getAllWithCache");
        Yii::$app->cache->delete("db-BaseAreas-getByIdWithCache/{$this->id}");
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        $this->deleteCaches();
    }

    public function afterRefresh()
    {
        parent::afterRefresh();
        $this->deleteCaches();
    }

    public function afterDelete()
    {
        parent::afterDelete();
        $this->deleteCaches();
    }


    # relations
    # static funs

    public function test()
    {
        $test = self::instance();
        $test->setScenario(self::SCENARIO_TEST);
        $test->save();
        var_dump($test->toArray());
    }

    # funs
}
